﻿using System;
using System.Collections.Generic;
using System.IO.IsolatedStorage;
using System.Linq;
using System.Reflection;
using System.Text;
using System.Threading.Tasks;

/**
 * 创建时间：2014/11/28 15:47:45
 * 作者：yong.blackcore
 * Email：itblackhole@outlook.com
 * QQ：50404503
 */
namespace ComicIsland.Common
{
    /// <summary>
    /// IsolatedStorageSettings是非线程安全的，多线程环境下对临界资源的访问应该互斥进行
    /// <para>警告，对于IsolatedStorageSettings的访问都应该通过本类，严禁直接访问。</para>
    /// </summary>
    public class ApplicationSettingHelper
    {
        private static object _sync = new object();

        /// <summary>
        /// 添加
        /// </summary>
        /// <param name="key"></param>
        /// <param name="value"></param>
        /// <returns></returns>
        public static bool Add(string key, object value)
        {
            bool result = false;
            try
            {
                if (!string.IsNullOrEmpty(key) && value != null)
                {
                    lock (_sync)
                    {
                        if (IsolatedStorageSettings.ApplicationSettings.Contains(key) == false)
                        {
                            IsolatedStorageSettings.ApplicationSettings.Add(key, value);
                            IsolatedStorageSettings.ApplicationSettings.Save();
                        }
                        else
                        {
                            IsolatedStorageSettings.ApplicationSettings[key] = value;
                            IsolatedStorageSettings.ApplicationSettings.Save();
                        }
                        result = true;
                    }
                }
            }
            catch (Exception exception)
            {
                LogHelper.LogError(exception.ToString(), typeof(ApplicationSettingHelper), MethodBase.GetCurrentMethod());
            }
            return result;
        }

        /// <summary>
        /// 返回字符串
        /// </summary>
        /// <param name="key"></param>
        /// <returns></returns>
        public static string GetString(string key)
        {
            string result = string.Empty;
            try
            {
                if (!string.IsNullOrEmpty(key))
                {
                    lock (_sync)
                    {
                        if (IsolatedStorageSettings.ApplicationSettings.Contains(key) == true)
                        {
                            result = IsolatedStorageSettings.ApplicationSettings[key].ToString();
                        }
                    }
                }
            }
            catch (Exception exception)
            {
                LogHelper.LogError(exception.ToString(), typeof(ApplicationSettingHelper), MethodBase.GetCurrentMethod());
            }
            return result;
        }

        /// <summary>
        /// 返回对象
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="key"></param>
        /// <returns></returns>
        public static T GetObject<T>(string key)
        {
            T result;
            try
            {
                if (!string.IsNullOrEmpty(key))
                {
                    lock (_sync)
                    {
                        if (IsolatedStorageSettings.ApplicationSettings.Contains(key) == true)
                        {
                            var settings = IsolatedStorageSettings.ApplicationSettings;
                            settings.TryGetValue(key, out result);
                            return result;
                        }
                    }
                }
            }
            catch (Exception exception)
            {
                LogHelper.LogError(exception.ToString(), typeof(ApplicationSettingHelper), MethodBase.GetCurrentMethod());

            }
            return default(T);
        }

        /// <summary>
        /// 删除
        /// </summary>
        /// <param name="key"></param>
        /// <returns></returns>
        public static bool Remove(string key)
        {
            bool result = false;
            try
            {
                if (!string.IsNullOrEmpty(key))
                {
                    lock (_sync)
                    {
                        if (IsolatedStorageSettings.ApplicationSettings.Contains(key) == true)
                        {
                            IsolatedStorageSettings.ApplicationSettings.Remove(key);
                            IsolatedStorageSettings.ApplicationSettings.Save();
                            result = true;
                        }
                    }
                }
            }
            catch (Exception exception)
            {
                LogHelper.LogError(exception.ToString(), typeof(ApplicationSettingHelper), MethodBase.GetCurrentMethod());

            }

            return result;
        }

        /// <summary>
        /// 是否存在
        /// </summary>
        /// <param name="key"></param>
        /// <returns></returns>
        public static bool IsExists(string key)
        {
            bool result = false;
            try
            {
                if (!string.IsNullOrEmpty(key))
                {
                    lock (_sync)
                    {
                        result = IsolatedStorageSettings.ApplicationSettings.Contains(key);
                    }
                }
            }
            catch (Exception exception)
            {
                LogHelper.LogError(exception.ToString(), typeof(ApplicationSettingHelper), MethodBase.GetCurrentMethod());
            }
            return result;
        }
    }
}
